#ifndef __SMARTHOMEMONITORSERVER_HPP__
#define __SMARTHOMEMONITORSERVER_HPP__

#include "EventLoop.hpp"
#include "Message.hpp"
#include "TcpServer.hpp"
#include "ThreadPool.hpp"
#include "BusinessTask.hpp"
#include <shadow.h>
#include <iostream>
#include <functional>
using std::cout;
using std::endl;

namespace wd{

class smartHomeMonitorServer{
public:
    smartHomeMonitorServer(int threadnum,int que_len,int port, string ip)
    :_threadpool(threadnum,que_len)
    ,_tcpserver(port,ip)
    {
    }
    void start(){
        using namespace std::placeholders;
        _tcpserver.setAllCallbacks(
                                   std::bind(&smartHomeMonitorServer::onConnection,this,_1),
                                   std::bind(&smartHomeMonitorServer::onMessage,this,_1),
                                   std::bind(&smartHomeMonitorServer::onClose,this,_1));
        _threadpool.start();
        _tcpserver.start();
    }

    void stop(){
        _threadpool.stop();
        _threadpool.stop();
    }

private:
    void onConnection(wd::TcpConnectionptr tcpconn){
        printf("a new clienfd connected.\n");

    }
    
    void onMessage(wd::TcpConnectionptr tcpconn){
        wd::Packet packet;
        int ret = tcpconn->receive(packet);
        printf("onMessage,recv %d bytes\n",ret);
        //区分不同的情况,交给子线程处理
        if(packet.type == wd::TASK_LOGIN_SECTION1){
            wd::UserLoginSection1 logintask(packet,tcpconn);
            _threadpool.addTask(std::bind(&wd::UserLoginSection1::process,logintask));
        }else if(packet.type == wd::TASK_LOGIN_SECTION2){
            wd::UserLoginSection2 logintask(packet,tcpconn);
            _threadpool.addTask(std::bind(&wd::UserLoginSection2::process,logintask));
        }
    }
    
    void onClose(wd::TcpConnectionptr tcpconn){
        printf("a clienfd closed.\n");
    }

private:
    ThreadPool _threadpool;
    wd::TcpServer _tcpserver;
};

}



#endif // __SMARTHOMEMONITORSERVER_HPP__

